//约束Http请求属性
import { HttpFn } from '../config/httpConfig'
import { defaultsVal } from '../config/defaulVal'
import setOption from '../config/setOption'
import { AjaxConfig } from '../request/ajax'
import { ajax } from '../request/ajax'
const request: Function = function (url: string, type: string, option: AjaxConfig) {
    option.url = url;
    option.type = type
    return this.request(option)
}

export class Http implements HttpFn {
    defaults = defaultsVal;
    creat: Function = function (arr: Array<Promise<any>>): Http {
        const axios: Http = new Http()
        return axios
    }
    all: Function = async function (arr: Array<Promise<any>>):Promise<any[]>{
        let data: Array<any> = [];
        for (let value of arr) {
            let val = await value;
            data.push(val)
        }
        return data
    }
    get: Function = function (url: string, option: Object = {}) {
        return request.call(this, url, "get", option)
    }
    post: Function = function (url: string, option: Object = {}) {
        return request.call(this, url, "post", option)
    }
    patch: Function = function (url: string, option: Object = {}) {
        return request.call(this, url, "patch", option)
    }
    put: Function = function (url: string, option: Object) {
        return request.call(this, url, "put", option)
    }
    request: Function = function (option: AjaxConfig) {
        var data: AjaxConfig = setOption(option);
        return ajax(data)
    }
}